package com.yhy;

import java.io.*;
import java.util.ArrayList;
import java.util.Set;


/**
 * describe:
 *
 * @author helloworldyu
 * @data 2017/11/16
 */
public class FileAndGraph {
	private static final String EDGE_FILE_HEAD = "Source,Target,Type,weight";
	private static final String NODE_FILE_HEAD = "id,label";

	/**
	 * 从文件读入一个图格式:
	 * Source,Target,Type,weight
	 * 2 0,4,Directed,1.0
	 * 3 1,3,Directed,1.0
	 *
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	public static IGraph readFile(String fileName) throws IOException {
		//打开文件
		File file = new File(fileName);

		BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

		IGraph graph = null;

		try {
			//判断报文头对不对
			String line = bufferedReader.readLine();
			if (!line.equals(EDGE_FILE_HEAD)) {
				throw new IllegalArgumentException("数据格式头不对");
			}

			//循环读数据,添加边
			while (null != (line = bufferedReader.readLine())) {
				String[] bodys = line.split(",");
				int source = Integer.parseInt(bodys[0]);
				int target = Integer.parseInt(bodys[1]);
				String directed = bodys[2];

				if( null == graph ){
					double weigth = Double.parseDouble(bodys[3]);
					if( "Directed".equals(directed) ){
						graph = new SparseGraph(true);
					}else if( "Undirected".equals(directed) ){
						graph = new SparseGraph(false);
					}else {
						throw new IllegalArgumentException("数据格式 Type 不对");
					}
				}

				Vertex sourceVer = new Vertex(source);
				Vertex targetVer = new Vertex(target);
				//添加到图中失败说明图中有相同的 ID 的点
				if( !graph.addVertex(sourceVer) ){
					sourceVer = graph.findInGraph(sourceVer);
				}
				if( !graph.addVertex(targetVer) ){
					targetVer = graph.findInGraph(targetVer);
				}

				graph.connect(sourceVer, targetVer);
			}
		} finally {
			if (null != bufferedReader) {
				bufferedReader.close();
			}
		}

		return graph;
	}

	/**
	 * 把一个图写入文件,生成两个csv 文件
	 * @param graph
	 * @param nodeFileName 点文件
	 * @param edgeFileName 边文件
	 * @throws IOException
	 */
	public static void writeFile(IGraph graph, String nodeFileName,String edgeFileName) throws IOException {
		//=========打开文件没有则创建============
		File nodeFile = new File(nodeFileName);
		if (! nodeFile.exists()) {
			 nodeFile.createNewFile();
		}
		File edgeFile = new File(edgeFileName);
		if (!edgeFile.exists()) {
			edgeFile.createNewFile();
		}

		//=========写文件======================
		BufferedWriter nodeBufferedWiter = null;
		BufferedWriter edgeBufferedWriter = null;
		try {
			//-----写文件头-----
			nodeBufferedWiter = new BufferedWriter(new FileWriter(nodeFile));
			nodeBufferedWiter.write(NODE_FILE_HEAD);
			nodeBufferedWiter.append('\n');

			edgeBufferedWriter= new BufferedWriter(new FileWriter(edgeFile));
			edgeBufferedWriter.write(EDGE_FILE_HEAD);
			edgeBufferedWriter.append('\n');
			//-----写图内容-----
			StringBuilder newNodeLine = new StringBuilder();
			StringBuilder newEdgeLine = new StringBuilder();

			Set<Vertex> vertexs = graph.getVertexs();
			boolean directed = graph.isDirected();
			for (Vertex v : vertexs) {
				//---------先清空---------
				newNodeLine.delete(0,newEdgeLine.length());
				//---------添加一行-------
				newNodeLine.append(v.id);
				newNodeLine.append(',');
				newNodeLine.append(v.id);
				newNodeLine.append('\n');
				nodeBufferedWiter.append(newNodeLine);

				ArrayList<Vertex> adj = graph.getAdj(v);
				for (Vertex adjVer : adj) {
					//------清空先--------
					newEdgeLine.delete(0, newEdgeLine.length());
					//------添加一行------
					newEdgeLine.append(v.getId());
					newEdgeLine.append(",");
					newEdgeLine.append(adjVer.getId());
					newEdgeLine.append(",");
					if( directed ){
						newEdgeLine.append("Directed");
					}else {
						newEdgeLine.append("Undirected");
					}

					newEdgeLine.append(",");
					newEdgeLine.append("1.0");
					newEdgeLine.append('\n');
					edgeBufferedWriter.append(newEdgeLine.toString());
				}
			}

		} finally {
			if( null != nodeBufferedWiter ){
				nodeBufferedWiter.close();
			}
			if( null != edgeBufferedWriter ){
				edgeBufferedWriter.close();
			}
		}
	}




}